<div class="clientarea"	>
<div style="float: right;margin-top: -38px"><?php echo getUserFullname();?>
    <?php $company=getUserCompany();
    if ($company!="None" && $company!="") {?>
    (<?php echo $company; ?>)
    <?php } ?></div>

<!-- <h1 ><?php echo $var_shop_clientarea; ?></h1> -->

<?php

//echo $portalId;

if (getParameter("key")){
$mail=TEAdecrypt(getParameter("key"), KEY);
//echo $mail;

if (acivateUser($mail)){
    $data=array();
    $data['link'] = "/?pageName=login";
    ShowSuccess(TemplateWithData($var_shop_notif_accountactive,$data));
    portalLogin($mail, null,null);//
}else{

ShowError($var_shop_accountnotfound);
}

}else{

	

$clientId=getShopClientId();


//ShowError("log".IsLoggedIn());

if ($clientId)
{

    if (!isset($app_shop_discount)) {
        $app_shop_discount=0;
        ShowError($var_shop_discountundefined);
    }


	$userMail=getObjByKey("portal_logins","mail","login",$_SESSION["portal_status_UserName"],"portalId",$portalId);
	//echo $userMail;
	if (!isAccountActive())
	{




        $data=array();
        $data["link"]="#";


        ShowError(TemplateWithData($var_shop_accountnotactive,$data));

    }else{




        if (getParameter("action")=="logout") {

            $_SESSION["login"]=null;
            header("Location: ".createNiceUrl("pageName","accueil"));
        }elseif (getParameter("action")=="validate_cart_success" && getcartCount()>0)
        {
            supprimePanier();

            $ref=getShopOrderObj(getParameter("orderId"), "ref");





		$host=EW_PORTAL_URL;
		if (isLocal())  $host=getPortalUrl().".app.com";
		$link="http://".$host."/client_area.html";


            $data=array();
            $data['username'] = getUserFullname();
            $data['reference'] = $ref;
            $data['link'] = $link;
           $msg=TemplateWithData($var_shop_mail_ordersaved,$data);

		sendMailTo($userMail, $msg);


            $data=array();
            $data['username'] = getUserFullname();
            $data['reference'] = $ref;
            $msg=TemplateWithData($var_shop_mail_ordersavedadmin,$data);
		    sendMailToAdmins($portalId,$msg);



	//notifier le client et l'admin
            $data=array();
            $data['portal'] = getPortalName();
	ShowSuccess(TemplateWithData($var_shop_ordersubmited,$data));


	}elseif (getParameter("action")=="validate_devis_success" && getcartCount()>0)
	{
		supprimePanier();

		$ref=getShopOrderObj(getParameter("orderId"), "ref");




	$host=EW_PORTAL_URL;
	if (isLocal())  $host=getPortalUrl().".app.com";
	$link="http://".$host;



    $data=array();
            $data['username'] = getUserFullname();
            $data['reference'] = $ref;
            $data['link'] = $link;
           $msg=TemplateWithData($var_shop_mail_quotationsaved,$data);

	sendMailTo($userMail, $msg);

        $data=array();
        $data['username'] = getUserFullname();
        $data['reference'] = $ref;
        $msg=TemplateWithData($var_shop_mail_quotationsavedadmin,$data);

	sendMailToAdmins($portalId,$msg);

	ShowSuccess($var_shop_quotationsaved);


	}

        if (getcartCount()>0)
        {

            ShowSuccess("<a href='".createNiceUrl("pageName","cart")."'>".$var_shop_backtocart."</a>");

        }

	$sSql="SELECT
shop_orderstatus.status_".$LANG." as status,
shop_order.ref,
shop_order.date,
shop_order.id,
shop_order.discount
FROM
shop_orderstatus
INNER JOIN shop_order ON shop_order.orderstatusId = shop_orderstatus.id
WHERE
shop_order.fromweb = 'Y' and  portalId=".$portalId." and clientId=".$clientId." ORDER BY date DESC;";

	//echo $sSql;

		$rs=$GLOBALS["conn"]->Execute($sSql);

									if ($rs && $rs->RecordCount()>0) {
										$i=0;
										?>


										<div class="top">
										</div>
										<table width="100%">
										<tr class="toptable">

										<td width="15px"></td>
										<td><?php echo $var_shop_status; ?></td>

										<td><?php echo $var_shop_Reference; ?></td>
												<td width="150px" ><?php echo $var_shop_Date; ?></td>
												<td width="40px" ><?php echo $var_shop_Details; ?></td>
												<td width="15px"></td>
										</tr>

										<?php
								while (!$rs->EOF)  {

									$id=$rs->fields["id"];
									$discount=$rs->fields["discount"];
									$class="odded";
			if ($i%2) $class="even";
			?>
			<tr class="line <?php echo $class; ?>">
									<td width="15px"></td>
									<td>
								<?php echo $rs->fields["status"]; ?>
								</td>

								<td>
								<?php echo $rs->fields["ref"]; ?>
								</td>
								<td>
								<?php echo  date("d/m/Y    H:i:s", strtotime($rs->fields["date"])); ?>
								</td>

								<td>
								<a  id="orderline_<?php echo $id; ?>"><img src="/portal/images/cart.png" /></a>
								</td>
								<td width="15px"></td>
									</tr>
								<tr class="line <?php echo $class; ?>" id="order_<?php echo $id; ?>" style="display:none;">
								<td width="15px"></td>
									<td colspan="4"><br/>
									<b><?php echo $var_shop_OrderDetails; ?></b><br/><br/>
									<table width="100%" class="box" >
									<thead>
									<tr>
									<td width="15px"></td>
<td><?php echo $var_shop_Item; ?></td>
<?php if ($app_shop_hasUnit=="yes")
									{?>
			<td><?php echo $var_shop_ShoeSize; ?></td>
			<?php }  ?>
<td><?php echo $var_shop_Quantity; ?></td>
<td><?php echo $var_shop_Price; ?></td>
									</thead>
									<?php

									if ($app_shop_hasUnit=="yes")
									{
									$sSql="SELECT
shop_orderline.quantity,
shop_orderline.id,
shop_orderline.articleId,
shop_articles.lib_$LANG as lib,
shop_articles.price,
shop_sizes.size
FROM
shop_orderline
INNER JOIN shop_articles ON shop_orderline.articleId = shop_articles.id
INNER JOIN shop_unit ON shop_orderline.unitId = shop_unit.id
INNER JOIN shop_sizes ON shop_unit.size = shop_sizes.id
WHERE shop_articles.portalId=$portalId and
shop_orderline.orderId = ".$rs->fields["id"];
                              }
									else{

										$sSql="SELECT
										shop_orderline.quantity,
										shop_orderline.id,
										shop_orderline.articleId,
										shop_articles.lib_$LANG as lib,
										shop_articles.price
										FROM
										shop_orderline
										INNER JOIN shop_articles ON shop_orderline.articleId = shop_articles.id
										WHERE shop_articles.portalId=$portalId and
										shop_orderline.orderId = ".$rs->fields["id"];
									}
								//	echo $sSql;
									$rschild=$GLOBALS["conn"]->Execute($sSql);
									$j=0;
									$total=0;
									while (!$rschild->EOF)  {

                                        $id=$rschild->fields["id"];
										//$price=$rs->fields["price"];
										$articleId=$rschild->fields["articleId"];
										$lib=$rschild->fields["lib"];
										$quantity=$rschild->fields["quantity"];
										if ($app_shop_hasUnit=="yes")
									{	$size=$rschild->fields["size"];}
										$price=$rschild->fields["price"];
										$class="odded2";
										$total+=$price*$quantity;
										if ($j%2) $class="even2";
                                     //   echo "id ".$id;
                                        $picture=getObjByKey("shop_gallery","picture","articleId",$articleId);
                                       // echo $picture;
										?>
											<tr class="line <?php echo $class; ?>" id="article_<?php echo $id; ?>">
											<td width="15px"></td>
										<td>
									<?php echo $lib; ?>

													<div  id="image_<?php echo $id; ?>" class="image">
													<img src="<?php echo getFilesPath("gallery",false)."/".$picture; ?>" width="120px"  />

													</div>
													</td>
													<?php if ($app_shop_hasUnit=="yes")
							{?>
			<td><?php echo $size; ?></td>
			<?php }  ?>

												<td><?php echo $quantity; ?></td>
												<td><?php echo $price; ?></td>
												<td><?php echo $price*$quantity; ?></td>
												<td width="15px"></td>
										</tr>
										<?php
										$j++;
									$rschild->MoveNext();
									}

									?>

									</table>
									<div>
									<?php if ($discount>0) {?>
									<div class="discount" >
                                        <?php echo $var_shop_discount; ?> : <?php echo $discount; ?> <?php echo $app_shop_currency; ?>
											</div><br/><br/>
											<?php } ?>
									<div class="total">
                                        <?php echo $var_shop_sumtopay; ?> : <?php echo $total-$discount; ?> <?php echo $app_shop_currency; ?>
											</div>
							</div>

									</td>
									<td width="15px"></td>
									</tr>
								<?php
								$i++;
									$rs->MoveNext();
								}

									?>

									</table>
									<div class="bottom">
									</div>
									<?php
									}else{

										ShowError($var_shop_noorder);


								}
    $promos=getObjByKey("shop_clients","promo","id",$clientId);
    if ($promos>0){
     echo "<br/>";
        $promos=$app_shop_discount*$promos;
        if (strpos($app_shop_discount,"%")>=0) $promos.="%";
       //ShowSuccess($var_shop_havediscount);
        $data=array();
        $data['promos'] = $promos;
      ShowSuccess(TemplateWithData($var_shop_havediscount,$data));
    }


	}

    ?>

    <br/>


    <div class="logout rounded"><a onclick="logout()"><?php echo $var_shop_logout; ?></a></div>
<?php
}else{
	if (IsLoggedIn()){
	
		ShowError($var_portal_noclientprofile);
	
	}else{
	header("Location: ".createNiceUrl("pageName","login"));
	exit;	
	}

}

}
?>



</div>

<script type="text/javascript">

$('tr[id^="article_"]').click(
		  function () {
			  $('div[id^="image_"]').hide();
			 var id=$(this).attr('id').replace("article_","");
			   $("#image_"+id).fadeIn();
			  }
			);




$('a[id^="orderline_"]').click(function () {

	$('tr[id^="order_"]').hide();
			 var id=$(this).attr('id').replace("orderline_","");
			   $("#order_"+id).slideDown("fast");
			  }
			);

/*
 *
 ,
 function () {
	  var id=$(this).attr('id').replace("orderline_","");
	   $("#order_"+id).fadeOut();
 }
 */


	</script>